package cn.stylefeng.guns.modular.gridsystem.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjectUtil;
import cn.hutool.json.JSONObject;
import cn.hutool.json.JSONUtil;
import cn.stylefeng.guns.core.exception.ServiceException;
import cn.stylefeng.guns.core.factory.PageFactory;
import cn.stylefeng.guns.core.pojo.page.PageResult;
import cn.stylefeng.guns.modular.gridsystem.entity.BizFaultModel;
import cn.stylefeng.guns.modular.gridsystem.mapper.BizFaultModelMapper;
import cn.stylefeng.guns.modular.gridsystem.model.param.BizFaultModelParam;
import cn.stylefeng.guns.modular.gridsystem.service.BizFaultModelService;
import cn.stylefeng.guns.modular.gridsystem.utils.EngineUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.ibatis.annotations.Param;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * biz_fault_model 服务实现类
 *
 * @author ssy
 * @date 2021/11/01 16:44
 */
@Service
public class BizFaultModelServiceImpl extends ServiceImpl<BizFaultModelMapper, BizFaultModel> implements BizFaultModelService {

    @Override
    public PageResult<BizFaultModel> page(BizFaultModelParam bizFaultModelParam) {

        // 构造条件
        LambdaQueryWrapper<BizFaultModel> queryWrapper = new LambdaQueryWrapper<>();
        // 查询分页结果
        return new PageResult<>(this.page(PageFactory.defaultPage(), queryWrapper));
    }

    @Override
    public List<BizFaultModel> list(BizFaultModelParam bizFaultModelParam) {

        // 构造条件
        LambdaQueryWrapper<BizFaultModel> queryWrapper = new LambdaQueryWrapper<>();
        if(ObjectUtil.isNotNull(bizFaultModelParam)){
            if(ObjectUtil.isNotEmpty(bizFaultModelParam.getId())){
                queryWrapper.eq(BizFaultModel::getId,bizFaultModelParam.getId());
            }
            if(ObjectUtil.isNotEmpty(bizFaultModelParam.getGroupId())){
                queryWrapper.eq(BizFaultModel::getGroupId,bizFaultModelParam.getGroupId());
            }
            if(ObjectUtil.isNotEmpty(bizFaultModelParam.getFaultLevel())){
                queryWrapper.eq(BizFaultModel::getFaultLevel,bizFaultModelParam.getFaultLevel());
            }
        }
        return this.list(queryWrapper);
    }

    @Override
    public void add(BizFaultModelParam bizFaultModelParam) {

        // 将dto转为实体
        BizFaultModel bizFaultModel = new BizFaultModel();
        BeanUtil.copyProperties(bizFaultModelParam, bizFaultModel);

        this.save(bizFaultModel);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void delete(BizFaultModelParam bizFaultModelParam) {
        this.removeById(bizFaultModelParam.getId());
    }

    @Override
    public void edit(BizFaultModelParam bizFaultModelParam) {

        // 根据id查询实体
        BizFaultModel bizFaultModel = this.queryBizFaultModel(bizFaultModelParam);

        // 请求参数转化为实体
        BeanUtil.copyProperties(bizFaultModelParam, bizFaultModel);

        this.updateById(bizFaultModel);
    }

    @Override
    public BizFaultModel detail(BizFaultModelParam bizFaultModelParam) {
        return this.queryBizFaultModel(bizFaultModelParam);
    }

    @Override
    public PageResult<Map<String, Object>> pageList(BizFaultModelParam param,Set<Integer> list) {
        return new PageResult<>(this.baseMapper.pageList(PageFactory.defaultPage(),param,list));
    }

    @Override
    public BizFaultModel getOneByAccessToken(String key) {
        // 构造条件
        LambdaQueryWrapper<BizFaultModel> queryWrapper = new LambdaQueryWrapper<>();
        queryWrapper.eq(BizFaultModel::getAccessToken,key);
        return this.baseMapper.selectOne(queryWrapper);
    }


    /**
     * 获取biz_fault_model
     *
     * @author ssy
     * @since 2021/11/01 16:44
     */
    private BizFaultModel queryBizFaultModel(BizFaultModelParam bizFaultModelParam) {
        BizFaultModel bizFaultModel = this.getById(bizFaultModelParam.getId());
        return bizFaultModel;
    }
}
